package com.yeung.hibernate;

import com.yeung.hibernate.dao.BookShopDao;
import com.yeung.hibernate.service.BookShopService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;

public class Main {
    private ApplicationContext context = null;
    private DataSource source;
    private BookShopDao bookShopDao;
    private BookShopService bookShopService;

    @Before
    public void init() {
        context = new ClassPathXmlApplicationContext("spring-hibernate.xml");
        source = context.getBean(DataSource.class);
        this.bookShopService = context.getBean(BookShopService.class);
        this.bookShopDao = context.getBean(BookShopDao.class);

    }

    @Test
    public void test() {
        System.out.println(source);
    }

    @Test
    public void 根据isbn获取书的价格() {

        System.out.println(bookShopDao.findBookPriceByIsbn(1001));

    }

    @Test
    public void 库存减1() throws Exception {
        bookShopDao.updateBookStock(1001);

    }

    @Test
    public void 更新余额() throws Exception {
        bookShopDao.updateUserAccount("yeung", 500);
    }

    @Test
    public void 支付测试() throws Exception {
        bookShopService.purchase("yeung", 1001);
    }

    public void 测试事务传播行为() {
        bookShopService.checkOut("yeung", new int[]{1001, 1003});
    }
}
